<%@ page language="java"
         import="com.ruohi.manage.DBType,com.ruohi.manage.PoolManager,com.ruohi.util.common.CommonDate,com.ruohi.util.common.CommonForm,com.ruohi.util.common.CommonString,com.ruohi.util.common.Env"
         pageEncoding="UTF-8" %>
<%@ page import="com.ruohi.util.dbutil.DBHelper" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.TreeMap" %>
<%
	Connection connection = null;
	DBHelper dbHelper = DBHelper.getInstance();
	Map model = new HashMap();
	Map categorys = new TreeMap();
	List<Map> productList = null;
	String tableName = "gift_product";
	String channelUUID = CommonString.getFormatPara(request.getParameter("channel_uuid"));
	Map adminInfo = (Map)session.getAttribute("adminInfo");
	
	try{
		connection = PoolManager.getConnection(DBType.getDBTypeByEnum("106013"));
		String id = CommonString.getFormatPara(request.getParameter("id"));
		if(null!=id&&!"".equals(id)){
			model = dbHelper.getMap(" select * from "+tableName+" where uuid = ? ",new Object [] {id} , connection);
		}
		if("".equals(channelUUID)&&null!=model){
			channelUUID = (String)model.get("channel_uuid");
		}
		
		String no = CommonString.getFormatPara(model.get("no"));
		if(no.equals("")){
			no = CommonDate.getToday("yyyyMMddHHmmss")+(int)(Math.random()*1000);
		}else{
			productList = dbHelper.getMapList("select product_no, order_no, is_main from gift_product_package_product where package_product_no='" + no + "' order by order_no desc, id", connection);
		}
		
		String resDomain = Env.getInstance().getProperty("img_domain");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><%=Env.getInstance().getProperty("project_name") %></title>
<link rel="stylesheet" type="text/css" href="/plugin/other/highslide/highslide.css" />
<link href="/plugin/ui/ligerUI/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<link href="/plugin/ui/ligerUI/ligerUI/skins/Gray/css/all.css" rel="stylesheet" type="text/css" />
<link href="/plugin/ui/ligerUI/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
<link href="/backend/style/style.css" rel="stylesheet" type="text/css" />
<script src="/plugin/jquery/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="/plugin/area/city/citys.js"></script>
<script type="text/javascript" charset="utf-8" src="/plugin/editor/ueditor/editor_config.js"></script>
<script type="text/javascript" charset="utf-8" src="/plugin/editor/ueditor/editor_all_min.js"></script>
<link rel="stylesheet" type="text/css" href="/plugin/editor/ueditor/themes/default/ueditor.css"/>
<style type="text/css">
.shade { filter:alpha(opacity=30); -moz-opacity:0.3; -khtml-opacity: 0.3; opacity: 0.3; background-color:#fff; position: relative; width: 130px; height: 135px; z-index: 4; display: none; }
.blue { border: 1px solid blue; }
.ccc { border: 1px solid #ccc; }
.toobar { position: absolute; bottom: 0px; height: 20px; width: 130px; border-top: 1px solid #ccc; }
.single_img { border: 1px solid #ccc; width: 130px; height: 135px; float:left; margin: 10px 0 10px 10px; display:inline; position:relative; cursor: pointer; overflow: hidden; }
.single_img .show_img { position:absolute; bottom: 20px; }
.formRight { text-align:left;}
</style>
</head>

<body style="margin: 0;padding: 0; overflow-y:auto; *overflow-y:hidden;">
<form name="theform" id="theform" action="/backend/action/crud.jsp?enumNo=106013">
  <div class="formDiv" style="position:relative; text-align:left;">
  	<div class="group"> <span><img src="/plugin/ui/ligerUI/ligerUI/skins/icons/communication.gif" /></span> <span class="groupTitle">套餐产品</span><span style="margin-left:30px;"><a href="javascript://void(0);" onclick="jrWindow('/backend/gift.21-sun.com/gift_product/gift_product_list.jsp', '查看所有商品', 800, 600);">查看商品</a></span> </div>
  	<div class="formLeft"></div>
    <div class="formRight" style="width:420px; height:auto;">
      <table id="product_table" style="width:460px;" align="left">
      	<tr>
      		<th style="width:170px; text-align:center;">商品编号</th>
      		<th style="width:10px;"></th>
      		<th style="width:130px; text-align:center;">排序号</th>
      		<th style="width:50px; text-align:center;">主商品</th>
      		<th style="width:100px; text-align:center;"></th>
      	</tr>
      	<%
      		if(productList!=null && productList.size()>0){
      			for(int i=0; i<productList.size(); i++){
      	%>
      	<tr>
      		<td style="text-align:center;"><input type="text" id="p<%=i %>" class="jr_text" value="<%=CommonString.getFormatPara(productList.get(i).get("product_no")) %>" /></td>
      		<td></td>
      		<td style="text-align:center;"><input type="text" id="o<%=i %>" class="jr_text" value="<%=CommonString.getFormatPara(productList.get(i).get("order_no")) %>" /></td>
      		<td style="text-align:center;"><input type="radio" id="m<%=i %>"<%=CommonString.getFormatInt(productList.get(i).get("is_main"))==1 ? " checked" :"" %> name="is_main" /></td>
      		<td><a href="javascript://void(0);" onclick="del_product_tr(this);" class="attr_btn">删除</a></td>
      	</tr>
      	<%	
      			}
      		}else{
      	%>
      	<tr>
      		<td style="text-align:center;"><input type="text" id="p0" class="jr_text" /></td>
      		<td></td>
      		<td style="text-align:center;"><input type="text" id="o0" class="jr_text" /></td>
      		<td style="text-align:center;"><input type="radio" id="m0" name="is_main" /></td>
      		<td><a href="javascript://void(0);" onclick="del_product_tr(this);" class="attr_btn">删除</a></td>
      	</tr>
      	<%
      		}
      	%>
      </table>
      <div style="clear:both;"><a href="javascript://void(0);" onclick="add_product_tr();" class="attr_btn">增加商品</a></div>
    </div>
    <div style="clear:left;"></div>
    <div class="group"> <span><img src="/plugin/ui/ligerUI/ligerUI/skins/icons/communication.gif" /></span> <span class="groupTitle">基础信息</span> </div>
    <div class="formLeft">是否组合套装</div>
    <div class="formRight" style="width:190px">
      <label>是</label>
    </div>
    <div style="clear:left;"></div>
    <div class="formLeft"><span style="color:#F00">* </span>套装名称</div>
    <div class="formRight" style="width:190px">
      <input type="text" dataType="Require" msg="请输入套装名称" class="jr_text" name="zd_name" id="zd_name" value="<%=CommonString.getFormatPara(model.get("name")) %>" maxlength="50"/>
    </div>
    <div class="formLeft"><span style="color:#F00">* </span>套装编号(自动)</div>
    <div class="formRight" style="width:190px">
      <input type="text" name="zd_no" class="jr_text" readonly="readonly" id="zd_no" value="<%=no %>"/>
    </div>
    <div style="clear:left;"></div>
    <div class="formLeft">库存量</div>
    <div class="formRight" style="width:190px">
      <input type="text" class="jr_text" name="zd_storage" id="zd_storage" value="<%=CommonString.getFormatPara(model.get("storage")) %>" maxlength="50"/>
    </div>
    <div class="formLeft">库存预警线</div>
    <div class="formRight" style="width:190px">
      <input type="text" class="jr_text" name="zd_storage_warning" id="zd_storage_warning" value="<%=CommonString.getFormatPara(model.get("storage_warning")) %>" maxlength="50"/>
    </div>
    <div style="clear:left;"></div>
    <div class="formLeft"><span style="color:#F00">* </span>市场价</div>
    <div class="formRight" style="width:190px">
      <input type="text" class="jr_text" name="zd_price" id="zd_price" value="<%=CommonString.getFormatPara(model.get("price")) %>" maxlength="50" dataType="Require" msg="请输入参考价" />
    </div>
    <div class="formLeft"><span style="color:#F00">* </span>商城价</div>
    <div class="formRight" style="width:190px">
      <input type="text" class="jr_text" name="zd_price_promotion" id="zd_price_promotion" value="<%=CommonString.getFormatPara(model.get("price_promotion")) %>" maxlength="50" dataType="Require" msg="请输入促销价" />
    </div>
    <div style="clear:left;"></div>
    <div class="formLeft"><span style="color:#F00">* </span>重量(带包装)</div>
    <div class="formRight" style="width:190px; text-align:left;">
      <input type="text" class="jr_text" name="zd_weight" id="zd_weight" style="width:80px;" value="<%=CommonString.getFormatPara(model.get("weight")).equals("")?"0.0":CommonString.getFormatPara(model.get("weight")) %>" maxlength="50"  dataType="Require" msg="请输入商品重量" />KG
    </div>
    <div style="clear:left;"></div>
    <div class="group"> <span><img src="/plugin/ui/ligerUI/ligerUI/skins/icons/communication.gif" /></span> <span class="groupTitle">扩展信息</span> </div>
    <div class="formLeft">是否上架</div>
    <div class="formRight" style="width:190px"> <%=CommonForm.createIsRadio(CommonString.getFormatPara(model.get("is_show")),"zd_is_show") %> </div>
    <div class="formLeft">上架时间</div>
    <div class="formRight" style="width: 150px;">
      <%
			String nowDate = CommonDate.getFormatDate("yyyy-MM-dd HH:mm",model.get("pub_date"));
			if("".equals(nowDate)){
				nowDate = CommonDate.getToday("yyyy-MM-dd HH:mm:ss");
			}
		%>
      <input type="text" class="Wdate" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'});" name="zd_pub_date" id="zd_pub_date" value="<%=nowDate %>" />
    </div>
    <div style="clear:left;"></div>
  </div>
  <input type="hidden" name="tableName" id="tableName" value="<%=tableName %>" />
  <input type="hidden" name="zd_id" id="zd_id" value="<%=CommonString.getFormatPara(model.get("id")) %>" />
  <input type="hidden" name="zd_order_no" id="zd_order_no" value="<%=CommonString.getFormatPara(model.get("order_no")) %>" />
  <input type="hidden" name="zd_channel_uuid" id="zd_channel_uuid" value="<%=channelUUID %>" />
  <input type="hidden" name="zd_is_package" id="zd_is_package" value="1" />
  <input type="hidden" name="zd_is_cus" id="zd_is_cus" value="0" />
  <input type="hidden" name="zd_is_rec" id="zd_is_rec" value="0" />
  <input type="hidden" name="zd_view_count" id="zd_view_count" value="0" />
  <input type="hidden" name="zd_price_agent" id="zd_price_agent" value="0.0" />
  <input type="hidden" name="zd_price_staff" id="zd_price_staff" value="0.0" />
  <input type="hidden" name="zd_min_buy_count" id="zd_min_buy_count" value="1" />
  <input type="hidden" name="hd_product" id="hd_product" />
  <input type="hidden" name="eventAfter" id="eventAfter" value="" />
</form>
</body>
</html>
<script type="text/javascript" src="/plugin/jquery/jquery.form.js"></script>
<script src="/plugin/ui/ligerUI/ligerUI/js/ligerui.min.js" type="text/javascript"></script>
<script src="/backend/scripts/common.js?tab" type="text/javascript"></script>
<script src="/plugin/validator/wofoshan/validator.min.js" type="text/javascript"></script>
<script type="text/javascript" src="/plugin/other/highslide/highslide.min.js"></script>
<script type="text/javascript" src="http://img.52sdskj.com/plugin/upload/jr_upload.js"></script>
<script src="/plugin/date/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
<script src="/plugin/ui/jqueryUI/jquery-ui-1.8.20.custom.min.js"></script>
<script type="text/javascript">		
	function doSub(dialog){
		setProduct();
		if($.trim($("#hd_product").val()) == ""){
			jrWarn("请选择套餐中的产品");
			return;
		}
		var productJson = eval($.trim($("#hd_product").val()));
		if(productJson.length < 2){
			jrWarn("请至少选择2件产品");
			return;
		}
		
		var rs = Validator.Validate(document.getElementById("theform"),1);
		if(rs){
			jQuery("#theform").ajaxSubmit({
				type : "POST",
				async : false ,
				success : function(data) {
					var rs = parseInt(jQuery.trim(data), 10);
					if (rs > 0) {
						$.ajax({
							type:"post",
							url:"/backend/action/ajax_r.jsp",
							data:{flag:"addPackageProduct", enumNo:"106013", package_product_no:"<%=no %>", product:$.trim($("#hd_product").val())},
							success:function(result){
								if($.trim(result) == "1"){
									window.parent.jQuery.ligerDialog.success("操作成功", "提示", function(){
										dialog.close();
									});
								}
							}
						});
						
						/*$.ajax({
							type:"post",
							url:"/backend/action/ajax_r.jsp",
							data:{flag:"generateProductIndex",no:$("#zd_no").val(),enumNo:"106013"}
						});*/
					}
				}
			});
		}
	}
	
	//增加自定义属性行
	function add_product_tr(){
		var tr_index = $("#product_table").find("tr").length - 1;
		var attr_tr_html = "<tr>";
		   attr_tr_html += "<td style=\"text-align:center;\"><input type=\"text\" id=\"p" + tr_index + "\" class=\"jr_text\" /></td>";
		   attr_tr_html += "<td></td>";
		   attr_tr_html += "<td style=\"text-align:center;\"><input type=\"text\" id=\"o" + tr_index + "\" class=\"jr_text\" /></td>";
		   attr_tr_html += "<td style=\"text-align:center;\"><input type=\"radio\" id=\"m" + tr_index + "\" name=\"is_main\" /></td>";
		   attr_tr_html += "<td><a href=\"javascript://void(0);\" onclick=\"del_product_tr(this);\" class=\"attr_btn\">删除</a></td>";
		   attr_tr_html += "</tr>";
		
		$("#product_table").append(attr_tr_html);
	}
	
	//删除自定义属性行
	function del_product_tr(obj){
		var tr_count = $("#product_table").find("tr").length;
		if(tr_count > 2){
			$(obj).parents("tr").remove();
		}
	}
	
	//合并页面上自定义文本框的值，转换为json存放到zd_attr隐藏字段中
	function setProduct(){
		var attr_item_str = "";
		$("#product_table").find("tr:gt(0)").each(function(){
			var p = $.trim($(this).find("input[id^='p']").val());
			var o = $.trim($(this).find("input[id^='o']").val());
			var m = $(this).find("input[id^='m']").is(":checked") ? 1 : 0;
			if(p!="" && o!=""){
				attr_item_str += "{'p':'" + p + "','o':'" + o + "', 'm':" + m + "},";
			}
		});
		
		if(attr_item_str != ""){
			attr_item_str = attr_item_str.substring(0, attr_item_str.length-1);
			attr_item_str = "[" + attr_item_str + "]";
			$("#hd_product").val(attr_item_str);
		}
	}
</script>
<%
	}catch(Exception e){
		e.printStackTrace();
	}finally{
		dbHelper.freeConnection(connection);
	}
%>